package com.bootssm.service.impl;

import com.bootssm.dao.HealthMapper;
import com.bootssm.entity.Sleep;
import com.bootssm.entity.Sports;
import com.bootssm.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class HealthServiceImpl implements HealthService {
    @Autowired
    private HealthMapper healthMapper;
    @Override
    public List<Sleep> findSleep(String id) {
        return healthMapper.findSleep(id);
    }

    @Override
    public void addSleep(Sleep sleep) {
        long time = new Date().getTime();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("MM-dd");
        String date = simpleDateFormat.format(time);
        sleep.setDate(date);
//        sleep.setUserId("1111");
        sleep.setId(String.valueOf(new Random().nextInt(10000000)));
        Integer bedtime = sleep.getBedtime();
        if (bedtime>=6&&bedtime<=8)sleep.setScore(10);
        else if (bedtime>8&&bedtime<=10)sleep.setScore(7);
        else sleep.setScore(4);
        healthMapper.addSleep(sleep);
    }

    @Override
    public void deleteSleep(String id) {
     healthMapper.deleteSleep(id);
    }

    @Override
    public Integer selectSleepScore(String userId) {
        return healthMapper.selectSleepScore(userId);
    }

    @Override
    public List<Sports> findSports(String userId) {
        return healthMapper.findSports(userId);
    }

    @Override
    public void addSports(String type,String time,String userId) {
        Sports sports = new Sports();
        sports.setUserId(userId);
        long time1 = new Date().getTime();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
         String date = simpleDateFormat.format(time1);
         sports.setDate(date);
        sports.setId(String.valueOf(new Random().nextInt(10000000)));
        sports.setExerciseTime(Integer.parseInt(time));
        sports.setExerciseType(type);
        healthMapper.addSports(sports);
    }

    @Override
    public void deleteSports(String id) {
        healthMapper.deleteSports(id);
    }

    @Override
    public Integer findSportsScore(String userId) {
        return healthMapper.findSportsScore(userId);
    }
}
